'use strict';

const puppeteer = require('puppeteer');
const http = require('http');

// (async () => {
//   const browser = await puppeteer.launch({headless: false});
//   const page = await browser.newPage();
//   await page.goto('https://github.com');
//   await page.screenshot({path: 'github.png'});

//   await browser.close();
// })();

// (async () => {
// 	const browser = await puppeteer.launch();
// 	const page = await browser.newPage();

// 	await page.setJavaScriptEnabled(true);
// 	await page.goto('https://www.baidu.com');
// 	const searchInput = await page.$('#kw');
// 	await searchInput.focus();
// 	await page.keyboard.type("puppeteer");
// 	const searchBtn = await page.$('#su');
// 	await searchBtn.click();
// 	await page.waitForSelector('#container');
// 	await page.setViewport({width:1920, height:1080});

// 	await page.screenshot({path: 'baidu.png'});
// 	await browser.close();
// })();

console.log("puppeteer is runing !!!")
var server = http.createServer(function(request,response){

	if (request.method == 'GET') {
		const body = "method not allowed!"
		response.statusCode = 403
		response.headers = {
			'Cache': 'no-cache',
        	'Content-Length': body.length
		}
		response.write(body)
		response.end()
		return 
	}
	console.log("这是method： "+request.method)
    console.log("这是postRaw： "+request.postRaw)
	// (async () => { 
	// const browser = await puppeteer.launch({headless: false}); 
	// const page = await browser.newPage(); 
	// await page.goto('https://baidu.com'); 
	// await page.waitFor(1000); 
	// await page.type('#kw', 'puppeteer', {delay: 100}); //打开百度后，自动在搜索框里慢慢输入puppeteer , 
	// page.click('#su') //然后点击搜索 
	// await page.waitFor(1000); 
	// const targetLink = await page.evaluate(() => {

	// let url =  document.querySelector('.result a').href
	// return url
	// }); 
	// console.log(targetLink); 
	// await page.goto(targetLink); 
	// // await page.waitFor(1000); 
	// browser.close(); 
	// })()

	console.log("puppeteer is runing !!!")
	res.writeHead(200,{'Content-Type':'text/html'});
	res.write('<head><meta charset="utf-8"/></head>');
	res.end("hello world!!!");
});

server.listen('4000','127.0.0.1');

 



